package com.wc.alorithm.string.trietree;

import java.util.*;

/**
 * @Author congege
 * @Description
 * @Date Created in 2023/12/2-15:27
 */
public class Main2 {
    static int N = 1000010;
    // 孩子数组
    static int[][] son = new int[N][26];
    // 重复单词的数量
    static int[] cnt = new int[N];
    // 编码
    static int idx = 0;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        for (int i = 0; i < n; i++) {
            char[] s = scanner.next().toCharArray();
            insert(s);
        }
        for (int i = 0; i < m; i++) {
            char[] s = scanner.next().toCharArray();
            System.out.println(query(s));
        }
    }

    /**
     * 字典树中存在的前缀个数
     * @param s 待查询的字符串
     * @return
     */
    private static int query(char[] s) {
        int ans = 0;
        int p = 0;
        for (char c : s) {
            if (son[p][c - 'a'] == 0) {
                break;
            }
            p = son[p][c - 'a'];
            ans += cnt[p];
        }
        return ans;
    }

    private static void insert(char[] s) {
        int p = 0;
        for (char c : s) {
            if (son[p][c - 'a'] == 0) {
                son[p][c - 'a'] = ++idx;
            }
            p = son[p][c - 'a'];
        }
        cnt[p]++;
    }
}
